#!/bin/sh
MySqlRootPassword=''
SlaveIp=''
BackupRoot=/root/backup
SysDB="""information_schema
mysql
test"""

PATH=/bin:/usr/bin:$PATH
export PATH

MysqlRoot="$BackupRoot/mysql"
if [ ! -d $MysqlRoot ]; then
    mkdir -p $MysqlRoot
fi

SvnRoot="$BackupRoot/svn"
if [ ! -d $SvnRoot ]; then
    mkdir -p $SvnRoot
fi

BugsRoot="$BackupRoot/bugs"
if [ ! -d $BugsRoot ]; then
    mkdir -p $BugsRoot
fi

for database in `mysql -uroot -p$MySqlRootPassword -e "show databases;" | awk 'NF = 3' | sed -n '1!p'`
do 
    echo "$SysDB" | grep -q $database
    if [ $? -ne 0 ]; then
        echo -e "Backuping database '$database' ...\c"
        mysqlhotcopy -u root -p $MySqlRootPassword $database $MysqlRoot --addtodest &>/dev/null
        if [ $? -eq 0 ]; then
            echo -e "Done!"
        else
            echo -e "Fail!"
        fi
    fi
done

cd /var/www/svn
for repos in `ls /var/www/svn`
do 
    echo -e "Backuping svn repos '$repos' ...\c"
    SvnReposDir=$SvnRoot/$repos
    rm -rf $SvnReposDir
    mkdir -p $SvnReposDir
    svnadmin hotcopy $repos $SvnReposDir
    if [ $? -eq 0 ]; then
        echo -e "Done!"
    else
        echo -e "Fail!"
    fi
done

echo -e "Backuping bugs data directory ...\c"
rsync -av /var/www/html/bugs/data $BugsRoot/ &>/dev/null
if [ $? -eq 0 ]; then
    echo -e "Done!"
else
    echo -e "Fail!"
fi

cp /etc/svn-access-control  $BackupRoot/
cp /etc/svn-auth-users $BackupRoot/

echo -e "Transfer Local Backup into Slave Machine...\c"
rsync --delete -ae ssh $BackupRoot/  root@$SlaveIp:$BackupRoot/
if [ $? -eq 0 ]; then
    echo -e "Done!"
else
    echo -e "Fail!"
fi
